head,n = input().split()
nodes={}

for i in range(int(n)):
    node,val,next=input().split()
    nodes[node]=[val,next]

linklist=[]

while  head !="-1":

    linklist.append(nodes.get(head)[0])
    print(linklist)
    head=nodes.get(head)[1]


length=len(linklist)
mid=int(length/2)
print(linklist[mid])



